package com.stylefeng.guns.modular.system.api;

import com.stylefeng.guns.core.base.controller.BaseController;
import com.stylefeng.guns.core.response.JsonResult;
import com.stylefeng.guns.core.response.ResultGenerator;
import com.stylefeng.guns.core.util.IpUtil;
import com.stylefeng.guns.modular.system.model.Jarinfo;
import com.stylefeng.guns.modular.system.model.vo.JInfo;
import com.stylefeng.guns.modular.system.model.vo.JarinfoVo;
import com.stylefeng.guns.modular.system.service.IJarinfoService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@RestController
@RequestMapping
public class JarinfoApiController extends BaseController{

    @Autowired
    IJarinfoService jarInfoService;

    /**
     * 获取版本
     * */
    @RequestMapping(value = "/getJarVersion/{cpid}/{version_code}/{country}", produces="application/json;charset=UTF-8")
    public JarinfoVo getJarVersion(HttpServletRequest request, @PathVariable String cpid, @PathVariable String version_code, @PathVariable String country){

//        response.setContentType("text/html;charset=UTF-8");   //防止浏览器弹出下载框下载json数据
        JarinfoVo jarInfoVo = null;
        try {
            jarInfoVo = new JarinfoVo();

            Jarinfo jarInfo = jarInfoService.selectByCpid(cpid);
            if(jarInfo == null){
                jarInfoVo.setCode("0");
                return  jarInfoVo;
            }
//            System.out.println("version_code = " + version_code + "      " + jarInfo.getVersionCore());
            if(Integer.parseInt(version_code) < jarInfo.getVersionCore()){
                jarInfo.setCode("1");
                jarInfo.setResult("yes");
            }else {
                jarInfo.setResult("no");
                jarInfo.setCode("1");
            }

            BeanUtils.copyProperties(jarInfo,jarInfoVo);
            jarInfoVo.setVersion_code(jarInfo.getVersionCore() + "");


        } catch (Exception e) {
            jarInfoVo.setCode("0");
            e.printStackTrace();
        }

        return jarInfoVo;
    }


    @RequestMapping(value = "/jarInfo/{cpid}/{version_code}", produces="application/json;charset=UTF-8")
    public JInfo getJarInfo(@PathVariable String cpid, @PathVariable String version_code){

        JInfo jInfo = null;
        try {
            jInfo = new JInfo();

            Jarinfo jarInfo = jarInfoService.selectByCpid(cpid);
            if(jarInfo == null){
                jInfo.setCode("0");
                return  jInfo;
            }

            if(Integer.parseInt(version_code) < jarInfo.getVersionCore()){
                jInfo.setCode("1");
                jInfo.setjUp("yes");

            }else {
                jInfo.setjUp("no");
                jInfo.setCode("1");
            }

            jInfo.setAddTime(jarInfo.getAddTime());
            jInfo.setCpid(jarInfo.getCpid());
            jInfo.setjUrl(jarInfo.getAddress());
            jInfo.setjVer(jarInfo.getVersionCore());
            jInfo.setMd5(jarInfo.getMd5());
            jInfo.setSid(jarInfo.getSid());
        } catch (Exception e) {
            jInfo.setCode("0");
            e.printStackTrace();
        }

        return jInfo;
    }

}
